//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ASDEF_H__
#define __ASDEF_H__

// Zener address space
//
// * 0x80000000 +--------------------+         2GB  PROCESS_TOP
//              |    Reserved(4MB)   |
//   0x7FC00000 +--------------------+     2GB-4MB  PRIVATE_LIMIT,PROCESS_LIMIT,
//              |    Thread stack    |              THREAD_STACK_LIMIT
//              |      (508MB)       |
//              |     (4MB*127)      |
// * 0x60000000 +--------------------+       1.5GB  THREAD_STACK_BASE
//   0x5FFF0000 +--------------------+  1.5GB-64KB  VIRTUAL_ALLOC_LIMIT
//              |                    |
//              |    Virtual alloc   |
//              |    (384MB-128KB)   |
//   0x48010000 +--------------------+   1GB+128MB+64KB VIRTUAL_ALLOC_BASE
//   0x48000000 +--------------------+   1GB+128MB  USER_HEAP_LIMIT
//              | Process heap(128MB)|
// * 0x40000000 +--------------------+         1GB  PRIVATE_BASE, USER_HEAP_BASE
//   0x3FFF0000 +--------------------+    1GB-64KB  COMMON_LIMIT,DLL_IMAGE_LIMIT
//              |                    |
//              |   Load dll images  |
//              |    (892MB-64KB)    |
//   0x08400000 +--------------------+       132MB  DLL_IMAGE_BASE,
//              |   Load exe image   |              EXE_IMAGE_LIMIT
//              |  (EXE_IMAGE_SIZE)  |
//              |    (132MB-64KB)    |
//   0x00010000 +--------------------+        64KB  COMMON_BASE, EXE_IMAGE_BASE
//              |   Reserved(64KB)   |
// * 0x00000000 +--------------------+           0
//

// Zener address space for neptune
//
// * 0x80000000 +--------------------+         2GB  PROCESS_TOP
//              |    Reserved(4MB)   |
//   0x7FC00000 +--------------------+     2GB-4MB  PRIVATE_LIMIT,PROCESS_LIMIT,
//              |    Thread stack    |              THREAD_STACK_LIMIT
//              |      (127MB)       |
//              |     (1MB*127)      |
//   0x77D00000 +--------------------+   2GB-131MB  THREAD_STACK_BASE
//   0x77C00000 +--------------------+   2GB-132MB  VIRTUAL_ALLOC_LIMIT
//              |                    |
//              |    Virtual alloc   |
//              |    (124MB-64KB)    |
//   0x70010000 +--------------------+   1GB+768MB+64KB VIRTUAL_ALLOC_BASE
//   0x70000000 +--------------------+   1GB+768MB  USER_HEAP_LIMIT
//              | Process heap(128MB)|
//   0x68000000 +--------------------+   1GB+640MB  PRIVATE_BASE, USER_HEAP_BASE
//   0x67FF0000 +--------------------+   1GB+640MB-64KB  COMMON_LIMIT,DLL_IMAGE_LIMIT
//              |                    |
//              |   Load dll images  |
//              |    (508MB-64KB)    |
//   0x48400000 +--------------------+   1GB+132MB  DLL_IMAGE_BASE,
//              |   Load exe image   |              EXE_IMAGE_LIMIT
//              |  (EXE_IMAGE_SIZE)  |
//              |       (132MB)      |
// * 0x40000000 +--------------------+         1GB  COMMON_BASE, EXE_IMAGE_BASE
//              |    Reserved(1GB)   |
// * 0x00000000 +--------------------+           0
//

//
// The other definition is in \elastos\inc\elacfg.h
//
#define PROCESS_TOP     0x80000000                      // 2GB
#define PROCESS_LIMIT   (PROCESS_TOP - 0x400000)        // 2GB - 4MB

#define RESERVED_SIZE   0x10000                         // 64KB

#ifndef _neptune

    #define COMMON_BASE     0x10000                         // 64KB
    #define COMMON_LIMIT    (0x40000000 - RESERVED_SIZE)    // 1GB - 64KB
    #define COMMON_SIZE     (COMMON_LIMIT - COMMON_BASE)

    #define EXE_IMAGE_BASE      COMMON_BASE                 // 64KB
    #define EXE_IMAGE_LIMIT     0x08400000                  // 132MB
    #define EXE_IMAGE_SIZE      (EXE_IMAGE_LIMIT - EXE_IMAGE_BASE)

    #define DLL_IMAGE_BASE      EXE_IMAGE_LIMIT
    #define DLL_IMAGE_LIMIT     COMMON_LIMIT
    #define DLL_IMAGE_SIZE      (DLL_IMAGE_LIMIT - DLL_IMAGE_BASE)

    #define PRIVATE_BASE    0x40000000                      // 1GB
    #define PRIVATE_LIMIT   PROCESS_LIMIT                   // 2GB - 4MB
    #define PRIVATE_SIZE    (PRIVATE_LIMIT - PRIVATE_BASE)

    #define USER_STACK_SIZE     (0x400000 - PAGE_SIZE)      // 4MB - 4KB

    //
    // The other definition is in \elastos\inc\elacfg.h
    //
    #define USER_STACK_ALIGN    0x400000                    // 4MB

    //
    // The other definition is in \elastos\inc\elacfg.h
    //
    #define USER_HEAP_BASE  PRIVATE_BASE                    // 1GB
    #define USER_HEAP_SIZE  0x8000000                       // 128MB
    #define USER_HEAP_LIMIT (USER_HEAP_BASE + USER_HEAP_SIZE)

    #define VIRTUAL_ALLOC_BASE  (USER_HEAP_LIMIT + RESERVED_SIZE)
    #define VIRTUAL_ALLOC_LIMIT (0x60000000 - RESERVED_SIZE)    // 1.5GB - 64KB
    #define VIRTUAL_ALLOC_SIZE  (VIRTUAL_ALLOC_LIMIT - VIRTUAL_ALLOC_BASE)

    #define THREAD_STACK_BASE   0x60000000                  // 1.5GB
    #define THREAD_STACK_LIMIT  PRIVATE_LIMIT               // 2GB - 4MB
    #define THREAD_STACK_SIZE   (THREAD_STACK_LIMIT - THREAD_STACK_BASE)

#else // _neptune

    #define COMMON_BASE     0x40000000                      // 1GB
    #define COMMON_LIMIT    (0x68000000 - RESERVED_SIZE)    // 1GB+640MB-64KB
    #define COMMON_SIZE     (COMMON_LIMIT - COMMON_BASE)

    #define EXE_IMAGE_BASE      COMMON_BASE                 // 1GB
    #define EXE_IMAGE_LIMIT     0x48400000                  // 1GB+132MB
    #define EXE_IMAGE_SIZE      (EXE_IMAGE_LIMIT - EXE_IMAGE_BASE)

    #define DLL_IMAGE_BASE      EXE_IMAGE_LIMIT
    #define DLL_IMAGE_LIMIT     COMMON_LIMIT
    #define DLL_IMAGE_SIZE      (DLL_IMAGE_LIMIT - DLL_IMAGE_BASE)

    #define PRIVATE_BASE    0x68000000                      // 1GB+640MB
    #define PRIVATE_LIMIT   PROCESS_LIMIT                   // 2GB - 4MB
    #define PRIVATE_SIZE    (PRIVATE_LIMIT - PRIVATE_BASE)

    #define USER_STACK_SIZE     (0x100000 - PAGE_SIZE)      // 1MB - 4KB

    //
    // The other definition is in \elastos\inc\elacfg.h
    //
    #define USER_STACK_ALIGN    0x100000                    // 1MB


    //
    // The other definition is in \elastos\inc\elacfg.h
    //
    #define USER_HEAP_BASE  PRIVATE_BASE                    // 1GB+640MB
    #define USER_HEAP_SIZE  0x8000000                       // 128MB
    #define USER_HEAP_LIMIT (USER_HEAP_BASE + USER_HEAP_SIZE)

    #define VIRTUAL_ALLOC_BASE  (USER_HEAP_LIMIT + RESERVED_SIZE)
    #define VIRTUAL_ALLOC_LIMIT 0x77C00000                  // 2GB-132MB
    #define VIRTUAL_ALLOC_SIZE  (VIRTUAL_ALLOC_LIMIT - VIRTUAL_ALLOC_BASE)

    #define THREAD_STACK_BASE   0x77D00000                  // 2GB-131MB
    #define THREAD_STACK_LIMIT  PRIVATE_LIMIT               // 2GB - 4MB
    #define THREAD_STACK_SIZE   (THREAD_STACK_LIMIT - THREAD_STACK_BASE)

#endif // _neptune

//
// User Memory Settings
// set by elastos.cfg
// default value in as.cpp
//
extern "C" Int32 g_LowMemoryThreshold;
extern "C" Int32 g_CriticallyLowMemoryThreshold;
extern "C" Int32 g_HeapGrow1stThreshold;
extern "C" Int32 g_HeapGrow2ndThreshold;
extern "C" Int32 g_RestoreHeapGrowThreshold;
//
// default value in as.cpp
//
extern "C" Int32 g_UserHeapGrowThreshold;
extern "C" Int32 g_UserStackGrowThreshold;

//
// The other definition is in \elastos\inc\elacfg.h
//
#define TLS_SIZE            (TLS_TOTAL_SLOTS * sizeof(PVoid))   // 256B
#define USER_STACK_PRESERVED_SIZE \
        (TLS_SIZE + ERROR_INFO_SIZE + 0x100)            // 1KB

//
// The other definition is in \elastos\inc\elacfg.h
//
#define PROXY_ENTRY_BASE            0x7ffef000

#define USER_STARTUP_ENTRY_BASE     0x7ffef800

//
// The definition is used for environment and device_path and redirect_info
// The other definition is in \elastos\inc\elacfg.h
//
#define USER_BLOCK_BASE             0x3fff8000          // 1GB - 32KB
#define USER_BLOCK_SIZE             0x4000              // 16KB

//
// Environment
// The other definition is in \elastos\inc\elacfg.h
//
#define USER_ENVIRONMENT_BASE   USER_BLOCK_BASE         // 1GB - 32KB
#define USER_ENVIRONMENT_SIZE   0x2000                  // 8KB
#define USER_ENVIRONMENT_LIMIT  (USER_ENVIRONMENT_BASE + USER_ENVIRONMENT_SIZE)

//
// The other definition is in \elastos\inc\elacfg.h
//                            \libpub\pubcrt\devpath.cpp
//
#define USER_DEVPATH_BASE       USER_ENVIRONMENT_LIMIT  // 1GB - 32KB + 8KB
#define USER_DEVPATH_SIZE       0x1A00                  // 6.5KB
#define USER_DEVPATH_LIMIT      (USER_DEVPATH_BASE + USER_DEVPATH_SIZE)

//
// The other definition is in \elastos\inc\elacfg.h
//
#define REDIRECT_INFO_BASE      USER_DEVPATH_LIMIT
#define REDIRECT_INFO_SIZE      0x600                   // 1.5KB
#define REDIRECT_INFO_LIMIT     (REDIRECT_INFO_BASE + REDIRECT_INFO_SIZE)

inline UInt32 AlignPageDown(UInt32 n)
{
    return n & PAGE_MASK;
}

inline UInt32 AlignPageUp(UInt32 n)
{
    return (n + PAGE_SIZE - 1) & PAGE_MASK;
}

inline UInt32 BytesToPages(UInt32 n)
{
    return AlignPageUp(n) >> PAGE_SHIFT;
}

inline UInt32 PagesToBytes(UInt32 n)
{
    return n << PAGE_SHIFT;
}

#define B_TO_P(b)           BytesToPages(b)
#define P_TO_B(p)           PagesToBytes(p)

#define FALL_INTO(addr, base, size) \
    ((addr) >= (base) && (addr) - (base) < (size))

#endif // __ASDEF_H__
